/*******************
Table 4. Heterogeneity: Effect of EITC Generosity on Self-Employment of Single, Less-educated Households, by Uber Availability
*******************/
use "data\hslesshousehold_ubereitc2005_2019", clear

foreach outcome in selfemp  {

	//female
	reghdfe `outcome' max_total_credit2020 max_total_credit2020_uber uber_exposed [pw = hhwt] if head_hsorless == 1 & hh_female == 1 & hh_married == 0, absorb(st_yr cbsa_id num_ekids) vce(cluster stfips) 
	
	matrix b_1`outcome' = e(b)
	matrix V_1`outcome' = e(V)
	scalar edf_r1`outcome' = e(df_r)
	scalar n_1`outcome' = e(N)
	qui sum `outcome' if e(sample) == 1 
	scalar dmean_1`outcome' = r(mean)
	
	//male
	reghdfe `outcome' max_total_credit2020 max_total_credit2020_uber uber_exposed [pw = hhwt] if head_hsorless == 1 & hh_male == 1 & hh_married == 0, absorb(st_yr cbsa_id num_ekids) vce(cluster stfips) 
	
	matrix b_2`outcome' = e(b)
	matrix V_2`outcome' = e(V)
	scalar edf_r2`outcome' = e(df_r)
	scalar n_2`outcome' = e(N)
	qui sum `outcome' if e(sample) == 1 
	scalar dmean_2`outcome' = r(mean)
	
	//single women with 1 or less
	reghdfe `outcome' max_total_credit2020 max_total_credit2020_uber uber_exposed [pw = hhwt] if head_hsorless == 1 & hh_female == 1 & hh_married == 0 & num_ekids<=1, absorb(st_yr cbsa_id num_ekids) vce(cluster stfips) 
	
	matrix b_3`outcome' = e(b)
	matrix V_3`outcome' = e(V)
	scalar edf_r3`outcome' = e(df_r)
	scalar n_3`outcome' = e(N)
	qui sum `outcome' if e(sample) == 1 
	scalar dmean_3`outcome' = r(mean)
	
	//single women with 0 or 2+
	reghdfe `outcome' max_total_credit2020 max_total_credit2020_uber uber_exposed [pw = hhwt] if head_hsorless == 1 & hh_female == 1 & hh_married == 0 & (num_ekids == 0 | num_ekids>1), absorb(st_yr cbsa_id num_ekids) vce(cluster stfips) 
	
	matrix b_4`outcome' = e(b)
	matrix V_4`outcome' = e(V)
	scalar edf_r4`outcome' = e(df_r)
	scalar n_4`outcome' = e(N)
	qui sum `outcome' if e(sample) == 1 
	scalar dmean_4`outcome' = r(mean)
	
	//single women with 1 or more
	reghdfe `outcome' max_total_credit2020 max_total_credit2020_uber uber_exposed [pw = hhwt] if head_hsorless == 1 & hh_female == 1 & hh_married == 0 & num_ekids>=1, absorb(st_yr cbsa_id num_ekids) vce(cluster stfips) 
	
	matrix b_5`outcome' = e(b)
	matrix V_5`outcome' = e(V)
	scalar edf_r5`outcome' = e(df_r)
	scalar n_5`outcome' = e(N)
	qui sum `outcome' if e(sample) == 1 
	scalar dmean_5`outcome' = r(mean)
	
	//nh white
	reghdfe `outcome' max_total_credit2020 max_total_credit2020_uber uber_exposed [pw = hhwt] if head_hsorless == 1 & hh_nh_white == 1 & hh_married == 0, absorb(st_yr cbsa_id num_ekids) vce(cluster stfips) 
	
	matrix b_8`outcome' = e(b)
	matrix V_8`outcome' = e(V)
	scalar edf_r8`outcome' = e(df_r)
	scalar n_8`outcome' = e(N)
	qui sum `outcome' if e(sample) == 1 
	scalar dmean_8`outcome' = r(mean)
	
	//nh black
	reghdfe `outcome' max_total_credit2020 max_total_credit2020_uber uber_exposed [pw = hhwt] if head_hsorless == 1 & hh_nh_black == 1 & hh_married == 0, absorb(st_yr cbsa_id num_ekids) vce(cluster stfips) 
	
	matrix b_9`outcome' = e(b)
	matrix V_9`outcome' = e(V)
	scalar edf_r9`outcome' = e(df_r)
	scalar n_9`outcome' = e(N)
	qui sum `outcome' if e(sample) == 1 
	scalar dmean_9`outcome' = r(mean)
	
	//nh other
	reghdfe `outcome' max_total_credit2020 max_total_credit2020_uber uber_exposed [pw = hhwt] if head_hsorless == 1 & hh_nh_other == 1 & hh_married == 0, absorb(st_yr cbsa_id num_ekids) vce(cluster stfips) 
	
	matrix b_10`outcome' = e(b)
	matrix V_10`outcome' = e(V)
	scalar edf_r10`outcome' = e(df_r)
	scalar n_10`outcome' = e(N)
	qui sum `outcome' if e(sample) == 1 
	scalar dmean_10`outcome' = r(mean)
	
	//hispanic
	reghdfe `outcome' max_total_credit2020 max_total_credit2020_uber uber_exposed [pw = hhwt] if head_hsorless == 1 & hh_hisp == 1 & hh_married == 0, absorb(st_yr cbsa_id num_ekids) vce(cluster stfips) 
	
	matrix b_11`outcome' = e(b)
	matrix V_11`outcome' = e(V)
	scalar edf_r11`outcome' = e(df_r)
	scalar n_11`outcome' = e(N)
	qui sum `outcome' if e(sample) == 1 
	scalar dmean_11`outcome' = r(mean)
	
	//18-54
	reghdfe `outcome' max_total_credit2020 max_total_credit2020_uber uber_exposed [pw = hhwt] if head_hsorless == 1 & (hh_age_18_34 == 1 | hh_age_35_54 == 1) & hh_married == 0, absorb(st_yr cbsa_id num_ekids) vce(cluster stfips) 
	
	matrix b_12`outcome' = e(b)
	matrix V_12`outcome' = e(V)
	scalar edf_r12`outcome' = e(df_r)
	scalar n_12`outcome' = e(N)
	qui sum `outcome' if e(sample) == 1 
	scalar dmean_12`outcome' = r(mean)
	
	//55-64
	reghdfe `outcome' max_total_credit2020 max_total_credit2020_uber uber_exposed [pw = hhwt] if head_hsorless == 1 & hh_age_55_64 == 1 & hh_married == 0, absorb(st_yr cbsa_id num_ekids) vce(cluster stfips) 
	
	matrix b_13`outcome' = e(b)
	matrix V_13`outcome' = e(V)
	scalar edf_r13`outcome' = e(df_r)
	scalar n_13`outcome' = e(N)
	qui sum `outcome' if e(sample) == 1 
	scalar dmean_13`outcome' = r(mean)
	
	
		//65+
	reghdfe `outcome' max_total_credit2020 max_total_credit2020_uber uber_exposed [pw = hhwt] if head_hsorless == 1 & hh_age_over64 == 1 & hh_married == 0, absorb(st_yr cbsa_id num_ekids) vce(cluster stfips) 
	
	matrix b_14`outcome' = e(b)
	matrix V_14`outcome' = e(V)
	scalar edf_r14`outcome' = e(df_r)
	scalar n_14`outcome' = e(N)
	qui sum `outcome' if e(sample) == 1 
	scalar dmean_14`outcome' = r(mean)
}

use "data\householdall_ubereitc2005_2019", clear

gen head_scollorless = head_hsorless == 1 | hh_somcoll
gen head_collorless = head_scollorless == 1 | hh_coll == 1

foreach outcome in selfemp  {

	//Some college or less
	reghdfe `outcome' max_total_credit2020 max_total_credit2020_uber uber_exposed [pw = hhwt] if head_scollorless == 1 & hh_married == 0, absorb(st_yr cbsa_id num_ekids) vce(cluster stfips) 
	
	matrix b_6`outcome' = e(b)
	matrix V_6`outcome' = e(V)
	scalar edf_r6`outcome' = e(df_r)
	scalar n_6`outcome' = e(N)
	qui sum `outcome' if e(sample) == 1 
	scalar dmean_6`outcome' = r(mean)
	
	//college or less
	reghdfe `outcome' max_total_credit2020 max_total_credit2020_uber uber_exposed [pw = hhwt] if head_collorless == 1 & hh_married == 0, absorb(st_yr cbsa_id num_ekids) vce(cluster stfips) 
	
	matrix b_7`outcome' = e(b)
	matrix V_7`outcome' = e(V)
	scalar edf_r7`outcome' = e(df_r)
	scalar n_7`outcome' = e(N)
	qui sum `outcome' if e(sample) == 1 
	scalar dmean_7`outcome' = r(mean)
}

//Table
cap file close regout
file open regout using "output\tab_eitcuber_het.tex", write replace
//Header
file write regout "\begin{tabular}{lccccccc}" _n
file write regout "\toprule" _n
file write regout "\toprule" _n
file write regout "& \multicolumn{7}{c}{Self-Employment} \\" _n
file write regout "& & & Female 0 & Female 0 & Female & Some & College  \\" _n
file write regout "& Female & Male & or 1 Kid & or 2+ Kids & 1+ Kids & College or Less & Degree or Less  \\" _n
file write regout "& (1) & (2) & (3) & (4) & (5 ) & (6) & (7) \\" _n
file write regout "\midrule" _n
//Table
file write regout "Maximum EITC (1000s)" 
foreach outcome in 1selfemp 2selfemp 3selfemp 4selfemp 5selfemp 6selfemp 7selfemp  {
	file write regout "&" %7.4f (b_`outcome'[1,1])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
foreach outcome in 1selfemp 2selfemp 3selfemp 4selfemp 5selfemp 6selfemp 7selfemp  {
	file write regout "&" "(" %5.4f ( sqrt(V_`outcome'[1,1]) ) ")"
}
file write regout "\\" _n
//Interaction
file write regout "Maximum EITC (1000s)*Uber in MSA" 
foreach outcome in 1selfemp 2selfemp 3selfemp 4selfemp 5selfemp 6selfemp 7selfemp {
	file write regout "&" %7.4f (b_`outcome'[1,2])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
foreach outcome in 1selfemp 2selfemp 3selfemp 4selfemp 5selfemp 6selfemp 7selfemp {
	file write regout "& " "(" %5.4f ( sqrt(V_`outcome'[2,2]) ) ")"
}
file write regout "\\" _n
//Uber
file write regout "Uber in MSA" 
foreach outcome in 1selfemp 2selfemp 3selfemp 4selfemp 5selfemp 6selfemp 7selfemp {
	file write regout " & " %7.4f (b_`outcome'[1,3])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,3]/(sqrt(V_`outcome'[3,3]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,3]/(sqrt(V_`outcome'[3,3]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,3]/(sqrt(V_`outcome'[3,3]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,3]/(sqrt(V_`outcome'[3,3]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,3]/(sqrt(V_`outcome'[3,3]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
foreach outcome in 1selfemp 2selfemp 3selfemp 4selfemp 5selfemp 6selfemp 7selfemp {
	file write regout "&" "(" %5.4f ( sqrt(V_`outcome'[3,3]) ) ")"
}
file write regout "\\" _n

file write regout "\\" _n
//Statistics
file write regout "Dependent Mean"
foreach outcome in 1selfemp 2selfemp 3selfemp 4selfemp 5selfemp 6selfemp 7selfemp {
	file write regout "&" %5.2f (`=scalar(dmean_`outcome')') 
}
file write regout "\\" _n
file write regout "Observations" 
foreach outcome in 1selfemp 2selfemp 3selfemp 4selfemp 5selfemp 6selfemp 7selfemp {
	file write regout "&" %12.0fc (`=scalar(n_`outcome')') 
}

file write regout "\\" _n
file write regout "\midrule" _n 
file write regout "& NH  & NH  & NH  & &  & &  \\" _n
file write regout "& White & Black & Other & Hispanic & 18-54 & 55-64 & Over 64  \\" _n
file write regout "\midrule" _n
//Table
file write regout "Maximum EITC (1000s)" 
foreach outcome in 8selfemp 9selfemp 10selfemp 11selfemp 12selfemp 13selfemp 14selfemp  {
	file write regout "&" %7.4f (b_`outcome'[1,1])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,1]/(sqrt(V_`outcome'[1,1]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
foreach outcome in 8selfemp 9selfemp 10selfemp 11selfemp 12selfemp 13selfemp 14selfemp  {
	file write regout "&" "(" %5.4f ( sqrt(V_`outcome'[1,1]) ) ")"
}
file write regout "\\" _n
//Interaction
file write regout "Maximum EITC (1000s)*Uber in MSA" 
foreach outcome in 8selfemp 9selfemp 10selfemp 11selfemp 12selfemp 13selfemp 14selfemp  {
	file write regout "&" %7.4f (b_`outcome'[1,2])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,2]/(sqrt(V_`outcome'[2,2]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
foreach outcome in 8selfemp 9selfemp 10selfemp 11selfemp 12selfemp 13selfemp 14selfemp {
	file write regout "& " "(" %5.4f ( sqrt(V_`outcome'[2,2]) ) ")"
}
file write regout "\\" _n
//Uber
file write regout "Uber in MSA" 
foreach outcome in 8selfemp 9selfemp 10selfemp 11selfemp 12selfemp 13selfemp 14selfemp {
	file write regout " & " %7.4f (b_`outcome'[1,3])
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,3]/(sqrt(V_`outcome'[3,3]))))) <.01 {
		file write regout "***"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,3]/(sqrt(V_`outcome'[3,3]))))) >=.01 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,3]/(sqrt(V_`outcome'[3,3]))))) <.05{
		file write regout "**"
	}
	if 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,3]/(sqrt(V_`outcome'[3,3]))))) >=.05 & 2*(ttail(`=scalar(edf_r`outcome')',abs(b_`outcome'[1,3]/(sqrt(V_`outcome'[3,3]))))) <.10{
		file write regout "*"
	}
}
file write regout "\\" _n
foreach outcome in 8selfemp 9selfemp 10selfemp 11selfemp 12selfemp 13selfemp 14selfemp {
	file write regout "&" "(" %5.4f ( sqrt(V_`outcome'[3,3]) ) ")"
}
file write regout "\\" _n

file write regout "\\" _n
//Statistics
file write regout "Dependent Mean"
foreach outcome in 8selfemp 9selfemp 10selfemp 11selfemp 12selfemp 13selfemp 14selfemp {
	file write regout "&" %5.2f (`=scalar(dmean_`outcome')') 
}
file write regout "\\" _n
file write regout "Observations" 
foreach outcome in 8selfemp 9selfemp 10selfemp 11selfemp 12selfemp 13selfemp 14selfemp {
	file write regout "&" %12.0fc (`=scalar(n_`outcome')') 
}

file write regout "\\" _n
file write regout "\bottomrule" _n
file write regout "\bottomrule" _n
file write regout "\end{tabular}"
file close regout